﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GitDiff
{
    class SystemParam
    {
        #region 变量
        private string m_newLineStr = "\r\n";
        public string NewLineStr
        {
            get
            {
                return m_newLineStr;
            }

            set
            {
                m_newLineStr = value;
            }
        }

        #endregion

        #region 方法
        /// <summary>
        ///  获取当前方法堆栈信息
        /// </summary>
        /// <returns></returns>
        public string GetStackTraceModelName()
        {
            //当前堆栈信息
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
            System.Diagnostics.StackFrame[] sfs = st.GetFrames();
            //过虑的方法名称,以下方法将不会出现在返回的方法调用列表中
            string _filterdName = "ResponseWrite,ResponseWriteError,Page_Load,ProcessRequestNotification,ProcessRequestNotificationHelper,ProcessRequestNotificationPrivate,BeginProcessRequestNotification,ResumeSteps,ExecuteStep,System.Web.HttpApplication.IExecutionStep.Execute,ProcessRequest,ProcessRequestMain,LoadRecursive";
            string _fullName = string.Empty, _methodName = string.Empty;
            for (int i = 1; i < sfs.Length; ++i)
            {
                //非用户代码,系统方法及后面的都是系统调用，不获取用户代码调用结束
                if (System.Diagnostics.StackFrame.OFFSET_UNKNOWN == sfs[i].GetILOffset()) break;
                _methodName = sfs[i].GetMethod().Name;//方法名称
                //sfs[i].GetFileLineNumber();//没有PDB文件的情况下将始终返回0
                if (_filterdName.Split(',').ToList().Exists(o=>o.ToString() == _methodName)) continue;
                _fullName = _methodName + "()->" + _fullName;
            }
            st = null;
            sfs = null;
            _filterdName = _methodName = null;
            return _fullName.TrimEnd('-', '>');
        }
        #endregion
    }
}
